package com.unit
{
	import mx.core.UIComponent;
	import mx.events.FlexEvent;

	public class AsGrid extends UIComponent
	{
		public function AsGrid()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, doCreateComplete);
		}

		protected function doCreateComplete(event : FlexEvent) : void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, doCreateComplete);
			fillGrid();
		}
		/** 网格线颜色 */
		[Bindable] public var lineColor : uint       = 0xCDC9C9; //
		/** 网格背景色 */
		[Bindable] public var gridFillColor : uint   = 0xFFFAF0; //
		/** 网格线粗细 */
		[Bindable] public var lineThickness : Number = 1; //
		/** 网格宽度大小(列宽),默认大小,如果没有指定的话 */
		[Bindable] public var gridHorSize : Number   = 50; //
		/** 网格高度大小(行高),默认大小,如果没有指定的话 */
		[Bindable] public var gridVerSize : Number   = 24; //
		/** 网络的行数*/
		[Bindable] public var horGridNum : uint      = 2; //
		/** 网络的列数*/
		[Bindable] public var verGridNum : uint      = 2; //
		/** 宽度定制,定制某一列的列宽,下标为0对应第0列的宽度值,如果值为-1表示用默认列宽,如果下标超过列数则忽略		 */
		[Bindable] public var horSizes : Array       = null;

		private var __width : Number                 = 0;
		private var __height : Number                = 0;

		/**
		 * 给舞台填充网格
		 */
		public function fillGrid() : void
		{
			__height = gridVerSize*horGridNum;
			for(var i : int = 0; i<verGridNum; i++)
			{
				var size : Number = gridHorSize;
				if(horSizes&&horSizes[i]&&(horSizes[i] as int!=-1))
				{
					size = horSizes[i] as int;
				}
				__width += size;
			}
			this.graphics.clear();
			this.graphics.beginFill(gridFillColor, 1);
			this.graphics.drawRect(0, 0, __width, __height);
			this.graphics.endFill();
			//填充背景色
			this.graphics.lineStyle(lineThickness, lineColor, 1);

			drawVerticalLine();
			drawHorizontalLine();
		}

		/**
		 * 画横线
		 */
		public function drawHorizontalLine() : void
		{
			var curPointy : Number = 0;
			for(var i : int; i<=horGridNum; i++)
			{
				this.graphics.moveTo(0, gridVerSize*i);
				this.graphics.lineTo(__width, gridVerSize*i);
			}
		}

		/**
		 * 画竖线
		 */
		public function drawVerticalLine() : void
		{
			this.graphics.moveTo(0, 0);
			this.graphics.lineTo(0, this.height);
			var drawPosition : Number = 0;
			for(var i : int = 0; i<verGridNum; i++)
			{
				var size : Number = gridHorSize;
				if(horSizes&&horSizes[i]&&(horSizes[i] as int!=-1))
				{
					size = horSizes[i] as int;
				}
				drawPosition += size;
				this.graphics.moveTo(drawPosition, 0);
				this.graphics.lineTo(drawPosition, this.height);
			}
		}
	}
}
